
<template>
  <div class="article-comments">
    <!-- 导航栏 -->
    <van-nav-bar :title="comment.reply_count + '条回复'" @click-left="$emit('close-reply')">
      <van-icon slot="left" name="cross" />
    </van-nav-bar>
    <!-- /导航栏 -->

    <!-- 当前评论 -->
    <van-cell title="当前评论">
      <van-image
        slot="icon"
        round
        width="30"
        height="30"
        style="margin-right: 10px;"
        :src="comment.aut_photo"
      />
      <span style="color: #466b9d;" slot="title">{{ comment.aut_name }}</span>
      <div slot="label">
        <p style="color: #363636;">{{ comment.content }}</p>
        <p>
          <span style="margin-right: 10px;">{{ comment.pubdate | relativeTime }}</span>
          <van-button
            size="mini"
            type="default"
          >回复 {{ comment.reply_count }}</van-button>
        </p>
      </div>
      <van-icon slot="right-icon" />
    </van-cell>
    <!-- /当前评论 -->

    <van-cell title="全部评论" />

    <!-- 回复列表 -->
      <van-list
        v-model="loading"
        :finished="finished"
        finished-text="没有更多了"
        @load="onLoad"
      >
      <van-cell
        v-for="item in list"
        :key="item.com_id.toString()"
      >
        <van-image
          slot="icon"
          round
          width="30"
          height="30"
          style="margin-right: 10px;"
          :src="item.aut_photo"
        />
        <span style="color: #466b9d;" slot="title">{{ item.aut_name }}</span>
        <div slot="label">
          <p style="color: #363636;">{{ item.content }}</p>
          <p>
            <span style="margin-right: 10px;">{{ item.pubdate | relativeTime }}</span>
            <van-button
              size="mini"
              type="default"
              @click="onReplyShow(item)"
            >回复</van-button>
          </p>
        </div>
        <van-icon
          slot="right-icon"
          color="red"
          :name="item.is_liking ? 'like' : 'like-o'"
          @click="onCommentLike(item)"
        />
      </van-cell>
    </van-list>
    <!-- 发布回复 -->
    <van-cell-group class="publish-wrap">
      <van-field
        v-model="inputComment"
        clearable
        placeholder="请输入评论内容"
      >
        <van-button
          slot="button"
          size="mini"
          type="info"
          @click="onAddComment"
        >发布</van-button>
      </van-field>
    </van-cell-group>
    <!-- /发布回复 -->
  </div>
</template>

<script>
import {
  getComments,
  addComment,
  addCommentLike,
  deleteCommentLike
} from '@/api/comment'

export default {
  name: 'comment-reply',
  props: {
    comment: {
      type: Object,
      required: true
    }
  },
  data () {
    return {
      list: [],
      loading: false,
      offset: null,
      finished: false,
      inputComment: ''
    }
  },
  methods: {
    async onLoad () {
      const res = await getComments({
        type: 'c',
        source: this.comment.com_id.toString(), // 这里都以大数字处理 避免未知错误
        offset: this.offset
      })
      this.list.push(...res.data.data.results)
      // 关闭加载效果
      this.loading = false
      // 判断是否是最后一条
      const lastId = res.data.data.last_id
      if (lastId) {
        this.offset = lastId
      } else {
        this.finished = true
      }
    },
    async onAddComment () {
      const inputComment = this.inputComment.trim()
      if (!inputComment) {
        return
      }
      const res = await addComment({
        target: this.comment.com_id, // 评论的目标id（评论文章即为文章id，对评论进行回复则为评论id）
        content: inputComment, // 评论内容
        art_id: this.$route.params.articleId // 文章id，对评论内容发表回复时，需要传递此参数，表明所属文章id。对文章进行评论，不要传此参数。
      })
      this.list.unshift(res.data.data.new_obj)
      this.inputComment = ''
    },
    async onCommentLike (comment) {
      if (comment.is_liking) {
        await deleteCommentLike(comment.com_id)
      } else {
        await addCommentLike(comment.com_id)
      }
      comment.is_liking = !comment.is_liking
    }
  }
}
</script>

<style scoped lang="less">
  .publish-wrap {
    position: fixed;
    bottom: 0;
    width: 100%;
    left: 0;
  }
  .van-list {
    margin-bottom: 45px;
  }
</style>
